package com.example.dormmanage.filter;


import com.example.dormmanage.service.impl.LoginUser;
import com.example.dormmanage.util.BaseContext;
import com.example.dormmanage.util.CustomerException;
import com.example.dormmanage.util.JwtUtil;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;
import org.springframework.web.filter.OncePerRequestFilter;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Component
public class JwtAuthenticationTokenFilter extends OncePerRequestFilter {

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
        //获取token
        String token = request.getHeader("token");
        if (token == null) {
            //放行让用户去登录
            filterChain.doFilter(request, response);
            return;
        }
        //解析token
        String userId = null;
        try {
            Claims claims = JwtUtil.parseJWT(token);
            userId = claims.getSubject();
        } catch (Exception e) {
            e.printStackTrace();
            throw new CustomerException("token非法");
        }
        //从redis中获取用户信息
        LoginUser loginUserByAdmin = (LoginUser) redisTemplate.opsForValue().get("login_admin_" + userId);
        LoginUser loginUserByStudent = (LoginUser) redisTemplate.opsForValue().get("login_student_"+ userId);
        if (loginUserByAdmin == null && loginUserByStudent == null){
            throw new CustomerException("用户未登录");
        }
        if(loginUserByAdmin != null && loginUserByStudent == null){
            BaseContext.setCurrentId(loginUserByAdmin.getAdmin().getAdminId());
            //存入SecurityContexHolder
            //TODO 获取权限信息分装到Authentication中
            UsernamePasswordAuthenticationToken authenticationToken =
                    new UsernamePasswordAuthenticationToken(loginUserByAdmin,null,loginUserByAdmin.getAuthorities());
            SecurityContextHolder.getContext().setAuthentication(authenticationToken);
        }
        if (loginUserByStudent != null && loginUserByAdmin == null){
            BaseContext.setCurrentId(loginUserByStudent.getStudent().getStudentId());
            //存入SecurityContexHolder
            //TODO 获取权限信息分装到Authentication中
            UsernamePasswordAuthenticationToken authenticationToken =
                    new UsernamePasswordAuthenticationToken(loginUserByStudent,null,loginUserByStudent.getAuthorities());
            SecurityContextHolder.getContext().setAuthentication(authenticationToken);
        }
        //放行
        filterChain.doFilter(request,response);
    }
}

